package com.example.demo.service;


import com.example.demo.pojo.Anime;
import com.example.demo.repository.AnimeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import java.util.List;

@Service
public class AnimeService {

    @Autowired
    private AnimeRepository animeRepository;

    // 获取所有动漫
    public List<Anime> getAllAnime() {
        return animeRepository.findAll();
    }

    // 根据 ID 获取动漫
    public Anime getAnimeById(Long id) {
        return animeRepository.findById(id).orElse(null);
    }

    // 添加动漫
    public Anime addAnime(Anime anime) {
        return animeRepository.save(anime);
    }

    // 更新动漫
    public Anime updateAnime(Long id, Anime animeDetails) {
        Anime anime = animeRepository.findById(id).orElse(null);
        if (anime != null) {
            anime.setTitle(animeDetails.getTitle());
            anime.setDescription(animeDetails.getDescription());
            anime.setImageUrl(animeDetails.getImageUrl());
            anime.setDetailsUrl(animeDetails.getDetailsUrl());
            return animeRepository.save(anime);
        }
        return null;
    }

    // 删除动漫
    public void deleteAnime(Long id) {
        animeRepository.deleteById(id);
    }

}
